package com.wk.springboot.writer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * MyFileWriter 用于将数据写入到文件的类
 *
 * @author xieyq
 * @version 1.0
 */
public class MyFileWriter {
    private final Logger LOG = LoggerFactory.getLogger(MyFileWriter.class);

    /**
     * 存储文件的路径
     */
    private String filePath;

    /**
     * 文件输出流对象
     */
    FileOutputStream out = null;

    /**
     * 输出流对象
     */
    OutputStreamWriter fw = null;

    /**
     * @param filePath 数据写入到文件的路径
     * @param append   追加模式
     * @throws Exception
     */
    public MyFileWriter(String filePath, boolean append) throws Exception {
        this.filePath = filePath;
        File file = new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }
        try {
            out = new FileOutputStream(file, append);
            fw = new OutputStreamWriter(out, "UTF-8");
        } catch (FileNotFoundException e) {
            LOG.error("FileNotFound ", e);
            throw new Exception("FileNotFound ", e);
        } catch (UnsupportedEncodingException e) {
            LOG.error("UnsupportedEncoding ", e);
            throw new Exception("UnsupportedEncoding ", e);
        }
    }

    public MyFileWriter(String filePath) throws Exception {
        this.filePath = filePath;
        try {
            out = new FileOutputStream(new File(filePath), false);
            fw = new OutputStreamWriter(out, "UTF-8");
        } catch (FileNotFoundException e) {
            LOG.error("FileNotFound ", e);
            throw new Exception("FileNotFound ", e);
        } catch (UnsupportedEncodingException e) {
            LOG.error("UnsupportedEncoding ", e);
            throw new Exception("UnsupportedEncoding ", e);
        }
    }

    /**
     * @return the filePath
     */
    public String getFilePath() {
        return filePath;
    }

    /**
     * @param filePath the filePath to set
     */
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    /**
     * 该方法是将数据封装到List中的数据写入到文件，并且按照utf8的格式写入到文件
     *
     * @param data 写入到文件的数据
     * @throws Exception
     */
    public void writeData(List<String> data) throws Exception, InterruptedException {
        try {
            if (data.isEmpty()) {
                return;
            }
            for (int i = 0; i < data.size(); i++) {
//                MonitorHelper.checkIn();
                String tmp = data.get(i);
                fw.append(tmp);
            }
            fw.flush();
        } catch (IOException e) {
            LOG.error("IOException ", e);
            throw new Exception("IOException ", e);
        }
    }

    public void writeData(String data) throws Exception {
        try {
            if (data == null || "".equals(data.trim())) {
                return;
            }
            fw.append(data);
            fw.flush();
        } catch (IOException e) {
            LOG.error("IOException ", e);
            throw new Exception("IOException ", e);
        }
    }

    /**
     * 关闭流方法
     */
    public void close() {
        try {
            if (null != fw) {
                fw.close();
            }
        } catch (IOException e) {
            LOG.error("FileOutputStream or OutputStreamWriter close error", e);
        }
        try {
            if (null != out) {
                out.close();
            }
        } catch (IOException e) {
            LOG.error("FileOutputStream or OutputStreamWriter close error", e);
        }
    }
}
